import {BasicColumn, FormSchema} from '/@/components/Table';
import {
  getAccountListAll,
  getAccountNameList,
  getAdminUserNameList,
  getAllRoleList
} from "/@/api/system/system";
import {h} from "vue";
import {Tag} from "ant-design-vue";
import {useUserStore} from "/@/store/modules/user";
import {formatToDateTime} from "/@/utils/dateUtil";
import {getTaskTypeList} from "/@/api/taskCenter/taskType";

const userStore = useUserStore();
const userInfo = userStore.getUserInfo;
const typeList = await getTaskTypeList();
const userList = await getAccountListAll({});
const roleList = await getAllRoleList()

const userInfoFull = userList.find(f => f.user_id == userInfo.userId)
const roleRemark = roleList.find(f => f.role_id == userInfoFull.role_id).remark

console.log('userInfo', userInfo)
console.log('roleRemark', roleRemark)
export const columns: BasicColumn[] = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 50,
    align: 'center',
  },
  {
    title: '申报人',
    dataIndex: 'fname',
    width: 100,
    align: 'center',
  }, {
    title: '任务标题',
    dataIndex: 'task_title',
    width: 250,
    align: 'center',
  },
  {
    title: '任务性质',
    dataIndex: 'is_temporary',
    width: 120,
    align: 'center',
    customRender: ({record}) => {
      const name = record.is_temporary === 1?'临时任务':'长期任务';
      const color = record.is_temporary === 1?'gray':'blue';
      return h(Tag, {color: color}, () => name);
    },
  },
  {
    title: '任务类型',
    dataIndex: 'task_type',
    width: 120,
    align: 'center',
    customRender: ({record}) => {
      const name = typeList.find(f => f.id == record.task_type)?.name || '无';
      return h(Tag, {color: 'blue'}, () => name);
    },
  },
  {
    title: '任务状态',
    dataIndex: 'status',
    width: 100,
    align: 'center',
    customRender: ({record}) => {
      const status = record.status;
      let color = 'blue';
      let val = '';
      switch (status) {
        case 3:
          color = 'red';
          break;
        case 4:
          color = 'red';
          break;
        case 5:
          color = 'green';
          break;
      }
      switch (status) {
        case 0:
          val = '初审中';
          break;
        case 1:
          val = '初审中';
          break;
        case 2:
          val = '二次审核中';
          break;
        case 3:
          val = '初审失败';
          break;
        case 4:
          val = '二次审核失败';
          break;
        case 5:
          val = '审核完成';
          break;
      }
      return h(Tag, {color: color}, () => val);
    },
  },
  {
    title: '申报奖励',
    dataIndex: 'task_reward',
    width: 350,
    align: 'center',
    customRender: ({record}) => {
      return record.task_reward + record.reward_unit
    },
  },
  {
    title: '任务申报时间',
    dataIndex: 'create_time',
    width: 180,
    align: 'center',
    customRender: ({record}) => {
      return formatToDateTime(record.update_time * 1000)
    },
  },
  {
    title: '附件数量',
    dataIndex: 'file_list',
    width: 180,
    align: 'center',
    customRender: ({record}) => {
      if (record.file_list) {
        return record.file_list.split(',').length;
      } else {
        return 0;
      }
    },
  },
  // {
  //   title: '任务修改时间',
  //   dataIndex: 'update_time',
  //   width: 180,
  //   align: 'center',
  //   customRender: ({ record }) => {
  //     return formatToDateTime(record.update_time*1000)
  //   },
  // },

  {
    title: '审核员',
    dataIndex: 'auditor_id',
    width: 100,
    align: 'center',
    customRender: ({record}) => {
      const name = userList.find(f => f.user_id == record.auditor_id)?.real_name || '无';
      return h(Tag, {color: 'blue'}, () => name);
    },
  },
  {
    title: '二次审核员',
    dataIndex: 'super_auditor_id',
    width: 100,
    align: 'center',
    customRender: ({record}) => {
      const name = userList.find(f => f.user_id == record.super_auditor_id)?.real_name || '无';
      return h(Tag, {color: 'blue'}, () => name);
    },
  },
  {
    title: '备注',
    dataIndex: 'tips',
    width: 160,
    align: 'center',
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'user_id',
    label: 'user_id',
    defaultValue: userInfo.userId,
    component: 'Input',
    colProps: {span: 8},
    show: false,
  },
  {
    field: 'id',
    label: '任务ID',
    component: 'Input',
    colProps: {span: 8},
  },
  {
    field: 'fname',
    label: '教师姓名',
    component: 'Input',
    colProps: {span: 8},
  },
  {
    field: 'fnumber',
    label: '教师工号',
    component: 'Input',
    colProps: {span: 8},
  },
  {
    field: 'roleRemark',
    label: '权限标识',
    defaultValue: roleRemark,
    component: 'Input',
    colProps: {span: 8},
    show: false,
  },
];
if (roleRemark === '超级管理员' || roleRemark === '二级审核员') {
  const data: FormSchema[] = [
    {
      field: 'status',
      label: '审核状态',
      component: 'Select',
      componentProps: {
        options: [
          { label: '二次审核中', value:2 },
          { label: '初审中', value: 1 },
          { label: '初审驳回', value: 3 },
          { label: '二次审核驳回', value: 4 },
          { label: '已完成', value: 5 },
          { label: '全部', value: -1 },
        ],
      },
      colProps: {span: 8},
    },
  ]
  searchFormSchema.push(...data)
}
//新增弹窗form字段
let defaultFormSchema: FormSchema[] = [
  {
    label: 'ID',
    field: 'id',
    component: 'Input',
    defaultValue: 0,
    show: false,
  },
  {
    label: 'task_type',
    field: 'task_type',
    component: 'Input',
    show: false,
  }, {
    label: 'open_id',
    field: 'open_id',
    component: 'Input',
    show: false,
  }, {
    label: 'order_id',
    field: 'order_id',
    component: 'Input',
    show: false,
  },
  {
    label: 'user_id',
    field: 'user_id',
    component: 'Input',
    defaultValue: userInfo.userId,
    show: false,
  },
  {
    field: 'task_id',
    label: '任务ID',
    component: 'Input',
    show: false,
  },
  {
    field: 'teacher_id',
    label: '教师id',
    component: 'Input',
    show: false,
  },
  {
    field: 'task_title',
    label: '任务标题',
    component: 'Input',
    componentProps: {
      disabled: true,
    },
    required: false,
  },

  {
    label: '审核员',
    field: 'auditor_id',
    component: 'ApiSelect',
    componentProps: {
      api: getAccountNameList,
      disabled: true,
      params: {},
      labelField: 'real_name',
      valueField: 'user_id',
    },
    required: false,
  },
  {
    label: '二次审核员',
    field: 'super_auditor_id',
    component: 'ApiSelect',
    componentProps: {
      disabled: true,
      api: getAdminUserNameList,
      params: {},
      labelField: 'real_name',
      valueField: 'user_id',
    },
    required: false,
  },

  {
    field: 'status',
    label: '状态',
    component: 'InputNumber',
    show: false,
  },
  {
    field: 'fname',
    label: '申报人',
    component: 'Input',
    componentProps: {
      disabled: true,
    },
    show: true,
  },
  {
    field: 'content',
    label: '申报内容',
    component: 'InputTextArea',
    componentProps: {
      disabled: true,
    },
    show: true,
  },
  {
    field: 'task_reward',
    componentProps: {
      disabled: true,
    },
    label: '任务规划奖励',
    component: 'InputNumber',
    required: false,
  },
  {
    label: '奖励单位',
    field: 'reward_unit',
    component: 'Input',
    componentProps: {
      disabled: true,
    },
    required: false,
  },
  {
    field: 'integral',
    label: '申报奖励',
    component: 'InputNumber',
    componentProps: {
      disabled: true,
    },
    required: false,
  },

];
if (roleRemark === '一级审核员') {
  const data: FormSchema[] = [
    {
      field: 'integral_first',
      label: '初审建议分数',
      component: 'Input',
      defaultValue: '',
      required: true,
    },
    {
      field: 'tips',
      label: '初审评语',
      component: 'InputTextArea',
      defaultValue: '',
    },
  ]
  defaultFormSchema.push(...data)
}
if (roleRemark === '超级管理员' || roleRemark === '二级审核员') {
  const data: FormSchema[] = [
    {
      field: 'integral_first',
      label: '初审建议分数',
      component: 'Input',
      defaultValue: '',
      required: true,
      componentProps: {
        disabled: true,
      },
    },
    {
      field: 'tips',
      label: '初审评语',
      component: 'InputTextArea',
      defaultValue: '',
      componentProps: {
        disabled: true,
      },
    },
    {
      field: 'integral_second',
      label: '终审评分',
      component: 'Input',
      defaultValue: '',
      required: true,
    },
    {
      field: 'tips_second',
      label: '终审评语',
      component: 'InputTextArea',
      defaultValue: '',
    },
  ]
  defaultFormSchema.push(...data)
}

export let formSchema: FormSchema[] = defaultFormSchema;
